/**
 * @author CCPalu
 */

function graphicAreaVariables(){
	var cnv = document.getElementById('CNV');
	var graphic = document.createElement('OBJECT');
	document.body.appendChild(graphic);
	
	graphic.name='graphicVariables';
	graphic.id='graphicVariables';

	graphic.samples = function(){
		return cnv.segData[1].length;
	};
	graphic.segmentWidth=12;
	graphic.segmentHeight=6;
	
	graphic.top = 360;
	graphic.generalLeft = 280;
	graphic.generalHeight = function(){
		return this.segmentHeight * this.samples();
	};
	
	graphic.labelWidth = 45;
	graphic.labelLeft = function(){
		return this.generalLeft;
	};
	graphic.CNVWidth = function(){
		return 600 + ((cnv.ch.length - 1) * (3400 / 21));
	}
	graphic.CNVLeft = function(){
		return this.labelLeft()+this.labelWidth - 17 ;
	};
	graphic.distanceWidth = 200;
	graphic.distanceLeft = function(){
		return this.CNVLeft()+this.CNVWidth()-19;
	};
	graphic.clinicalWidth = function(){
		return cnv.clinical.labels.length*this.segmentWidth*1.3;
	};
	graphic.clinicalLeft = function(){
		return this.distanceLeft()+this.distanceWidth+25*1;
	};
	
	return false;
}

function distanceChart(dist) {
	var cnv = document.getElementById('CNV');
	var ch = cnv.ch;
	var graphicVar = document.getElementById('graphicVariables');
	
	remove_element_by_id("simplechart");
	remove_element_by_id("node2");
	remove_element_by_id("chartLabel");
	remove_element_by_id("labelLegend");
	remove_element_by_id("distanceLegend");
	remove_element_by_id("blank1a");
	remove_element_by_id("blank1b");
	remove_element_by_id("blank1c");

	if (document.getElementById('CNV').similarity.distance!=undefined) {
		remove_element_by_id("chrefDiv");
	}
	var dc = dojox.charting;
	var dur = 450
	
	cnv.similarity.distance=	dist[0];
	cnv.similarity.sampleName=	dist[2];
	cnv.similarity.sampleNumber=	dist[1];
	var	method=document.getElementById('sim_method');
	cnv.similarity.metric=[method.options[method.selectedIndex].innerHTML, method.value];
	var item = document.getElementById("choose_sample");
	cnv.similarity.sample = [item.options[item.selectedIndex].innerHTML,item.value];
	cnv.similarity.chromosome= ch;

	for (var i = 0; i < dist[0].length; i = i + 1) {
		dist[0][i] = (Math.round(dist[0][i] * 100)) / 100;
	}
	
	var chart = document.createElement("div");
	chart.id = "simplechart";
	document.getElementById('chartingArea').appendChild(chart);
	chart.style.width = graphicVar.distanceWidth+'px';//"200px";
	chart.style.height = graphicVar.generalHeight()+52*1 + "px"
	chart.style.top = graphicVar.top -10 + "px";//"290px";
	chart.style.left = graphicVar.distanceLeft()+ "px";
	chart.style.position = "absolute";
	chart.style.zIndex=-20;

	var chart1 = new dojox.charting.Chart2D("simplechart");
	chart1.setTheme(dojox.charting.themes.PlotKit.cyan);
	document.getElementById("simplechart").style.hight = chart.style.height;
	chart1.addPlot("default", {
		type: "Bars"
	});
	chart1.addAxis("y", {
		includeZero: true
	});
	chart1.addAxis("x", {
		includeZero: true
	});
	chartLabel(organizeChart(dist[2],1));	
	chart1.addSeries("Series 1", dist[0]);

	clinicalMap(dist[1].reverse());
	cnvMap(dist[1]);
	try{	
		var animA = new dc.action2d.Highlight(chart1, "default", {
			duration: dur,
			easing: dojo.fx.easing.sineOut
		});
	}
	catch(e){}
	try{	
		var animB = new dc.action2d.Shake(chart1, "default");
	}
	catch(e){}
	try{
		var animC = new dc.action2d.Tooltip(chart1, "default");
	}
	catch(e){}
	try{
		var animD = new dc.action2d.Magnify(chart1, "default");
	}
	catch(e){}
	chart1.render();
	
	var distanceLegend=document.createElement('IMG');
	distanceLegend.id='distanceLegend';
	distanceLegend.src='http://cnviewer.googlecode.com/hg/Images/distance.png';
	document.getElementById('chartingArea').appendChild(distanceLegend);
	setPosition(distanceLegend,chart.style.left.replace('px',"")*1+chart.style.width.replace('px',"")*1-10, chart.style.top.replace('px',"")*1 + chart.style.height.replace('px',"")*1/2-15);
	distanceLegend.style.zIndex=-16;

	return false;	
}

function chartLabel(L, L2){
/* Creates a chart with the samples labels, when the use puts de mouse over
 * shows the sample identification.
 */
	var graphicVar= document.getElementById('graphicVariables');
	var dc = dojox.charting;
	var dur = 900;
	
	var chart = document.createElement("div");
	chart.id = "chartLabel";
	document.getElementById('chartingArea').appendChild(chart);
	chart.style.width = graphicVar.labelWidth + "px";
	chart.style.height = graphicVar.generalHeight()+graphicVar.segmentHeight*1+51*1 + "px";
	chart.style.top = graphicVar.top -15 + "px";
	chart.style.left = graphicVar.labelLeft() + "px";
	chart.style.position = "absolute";
	chart.style.zIndex=-21;
	
	var chartA = new dc.Chart2D("chartLabel");
	chartA.setTheme(dojox.charting.themes.PlotKit.cyan);
	chartA.addPlot("default", {
	type: "Default",
		lines: false,
		markers: true,
		tension: 2
	});
	chartA.addAxis("y", {
		includeZero: true
	});
	chartA.addAxis("x"	, {
		includeZero: true
	});
	
	if (L2)
		chartA.addSeries("Series 1", eval(L2));
	
	chartA.addSeries("Series 2", eval(L));
	try{
		var animC = new dc.action2d.Tooltip(chartA, "default");
	}
	catch (e){};
	try{
		var animB = new dc.action2d.Shake(chartA, "default");
	}
	catch (e){};
	try{
		var animD = new dc.action2d.Magnify(chartA, "default");	
	}
	catch (e){};

	chartA.render();

	var labelLegend=document.createElement('IMG');
	labelLegend.id='labelLegend';
	labelLegend.src='http://cnviewer.googlecode.com/hg/Images/samples.png';
	document.getElementById('chartingArea').appendChild(labelLegend);
	setPosition(labelLegend,graphicVar.labelLeft()-5, graphicVar.top + graphicVar.generalHeight()/2 - 15);
	labelLegend.style.zIndex=-15;
	
	var blank1=document.createElement('IMG');
	blank1.id='blank1a';
	blank1.src='http://cnviewer.googlecode.com/hg/Images/blank.png';
	document.getElementById('chartingArea').appendChild(blank1);
	setPosition(blank1,chart.style.left.replace('px',"")*1-6, graphicVar.top-20);

	var blank2=document.createElement('IMG');
	blank2.id='blank1b';
	blank2.src='http://cnviewer.googlecode.com/hg/Images/blank.png';
	document.getElementById('chartingArea').appendChild(blank2);
	setPosition(blank2,chart.style.left.replace('px',"")*1-8, graphicVar.generalHeight()*1+graphicVar.top*1);

	var blank3=document.createElement('IMG');
	blank3.id='blank1c';
	blank3.src='http://cnviewer.googlecode.com/hg/Images/blank.png';
	document.getElementById('chartingArea').appendChild(blank3);
	setPosition(blank3,chart.style.left.replace('px',"")*1-10, graphicVar.generalHeight()+15*1+graphicVar.top);
	
	return false;		
}

function organizeChart(index,x){
/* Creates the chart imput for labeling the samples */
	var label = ' [{x: ' + x + ', y: ' + 0 + ', tooltip: "--"}';

	for (var i = 1; i <= index.length; i = i + 1) {
		
		try {
			label = label + ', {x: ' + x + ', y: ' + i + ', tooltip: "' + index[i-1] + '"}';
		}
		catch (e){
			
		}
	}
	label = label + ', {x: ' + x + ', y: ' + i + ', tooltip: "--"}]';

	return label;
}

function organizeChart2(index,position,x){
	var lab = findInArray(position[1], index[0]);
	var label = ' [{x: ' + x + ', y: ' + (0.5 + lab) + ', tooltip: "' + position[2][lab] + '"}';

	for (var i = 1; i < index.length; i = i + 1) {
		lab = findInArray(position[i], index[0]);
		label = label + ', {x: ' + x + ', y: ' + (0.5 + lab) + ', tooltip: "' + position[2][lab] + '"}';
		
	}
	label = label + ']';

	return label;
}

function organizeChartA(index,x){

	var opt = document.getElementById('choose_sample').options;
	var lab;
	var label = ' [{x: ' + x + ', y: ' + -0.1 + '}';

	for (var i = 1; i <= index.length; i = i + 1) {
		lab = findInArray(opt, index[(i - 1)], 'value');
		try {
			label = label + ', {x: ' + x + ', y: ' + i + ', tooltip: "' + opt[lab].innerHTML + '"}';
		}
		catch (e){
			
		}
	}
	label = label + ']';

	return label;
}

function organizeChart2A(index,position,x){

	var opt = document.getElementById('choose_sample').options;
	//	var size=index.length-1;
	var lab = findInArray(opt, index[0],'value');
//	var label = ' [{x: ' + x + ', y: ' + 0 + '}';
	var label = ' [{x: ' + x + ', y: ' + (0.5 + findInArray(position, index[0])) + ', tooltip: "' + opt[lab].innerHTML + '"}';
	for (var i = 1; i < index.length; i = i + 1) {
		lab = findInArray(opt, index[(i)],'value');
		label = label + ', {x: ' + x + ', y: ' + (0.5 + findInArray(position, index[i])) + ', tooltip: "' + opt[lab].innerHTML + '"}';
		
	//	label = label + ', {x: ' + x + ', y: ' + findInArray(position, index[i]) + '}';	
	}
	label = label + ']';

	return label;
}

function createCanvas(w,h, id, parent){
	if (navigator.appName=="Microsoft Internet Explorer") {
		canvas = document.getElementById(id);
	}	
	else{
	if (parent) {
		var canvas = document.createElement("canvas");
		canvas.id = id;
		if (typeof parent == 'string'){
			parent=document.getElementById('parent');
		}
		parent.appendChild(canvas);
	}
	else {
		remove_element_by_id(id);
		var canvas = document.createElement("canvas");
		canvas.id = id;
		document.getElementById('chartingArea').appendChild(canvas);
		
	}
	}
	canvas.width=w;
	canvas.height=h;
	canvas.style.zIndex=-10;
	
	return canvas;
}
